<template>
  <div class="phones-page">
    <div class="page-header">
      <h1>华为手机</h1>
      <p>探索华为全系列智能手机</p>
    </div>

    <el-row :gutter="20">
      <el-col :span="6" v-for="phone in phones" :key="phone.id">
        <el-card class="phone-card" shadow="hover">
          <div class="product-image-container">
            <img :src="phone.image" :alt="phone.name" class="product-image">
            <div class="product-badge" v-if="phone.isNew">新品</div>
            <div class="product-badge discount" v-if="phone.discount">热销</div>
          </div>
          <div class="product-info">
            <h3>{{ phone.name }}</h3>
            <p class="product-desc">{{ phone.description }}</p>
            <div class="product-price">
              <span class="current-price">¥{{ phone.price }}</span>
              <span class="original-price" v-if="phone.originalPrice">¥{{ phone.originalPrice }}</span>
            </div>
            <div class="product-features">
              <span class="feature-tag" v-for="feature in phone.features" :key="feature">{{ feature }}</span>
            </div>
            <div class="product-actions">
              <el-button type="primary" size="small" @click="viewDetail(phone.id)">查看详情</el-button>
              <el-button type="success" size="small" @click="addToCart(phone)">加入购物车</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useCartStore } from '@/stores/cart'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const cartStore = useCartStore()
const authStore = useAuthStore()

const phones = ref([
  {
    id: 'mate70-pro-plus',
    name: 'HUAWEI Mate 70 Pro+',
    price: '7999',
    originalPrice: '8999',
    description: '超光变XMAGE影像，鸿蒙操作系统',
    image: 'https://via.placeholder.com/300x300/c7000a/FFFFFF?text=Mate70+Pro+',
    features: ['XMAGE影像', '鸿蒙OS', '北斗卫星'],
    isNew: true,
    discount: true
  },
  {
    id: 'p60-pro',
    name: 'HUAWEI P60 Pro',
    price: '6999',
    description: '超感知影像，艺术美学设计',
    image: 'https://via.placeholder.com/300x300/003366/FFFFFF?text=P60+Pro',
    features: ['超感知影像', '玲珑四曲屏', '艺术设计'],
    isNew: false,
    discount: true
  },
  {
    id: 'nova12',
    name: 'HUAWEI Nova 12',
    price: '2999',
    originalPrice: '3299',
    description: '潮流设计，年轻时尚',
    image: 'https://via.placeholder.com/300x300/FF6B6B/FFFFFF?text=Nova+12',
    features: ['潮流设计', '前置双摄', '轻薄机身'],
    isNew: true,
    discount: false
  },
  {
    id: 'mate-x5',
    name: 'HUAWEI Mate X5',
    price: '12999',
    description: '折叠屏旗舰，科技美学',
    image: 'https://via.placeholder.com/300x300/4ECDC4/FFFFFF?text=Mate+X5',
    features: ['折叠屏', '超轻薄', '多角度悬停'],
    isNew: false,
    discount: true
  },
  {
    id: 'mate50-pro',
    name: 'HUAWEI Mate 50 Pro',
    price: '6799',
    originalPrice: '7499',
    description: '超光变XMAGE影像，北斗卫星消息',
    image: 'https://via.placeholder.com/300x300/001f3f/FFFFFF?text=Mate50+Pro',
    features: ['XMAGE影像', '北斗卫星', '鸿蒙OS'],
    isNew: false,
    discount: true
  },
  {
    id: 'p50-pro',
    name: 'HUAWEI P50 Pro',
    price: '5988',
    description: '万象双环设计，原色影像',
    image: 'https://via.placeholder.com/300x300/666666/FFFFFF?text=P50+Pro',
    features: ['原色影像', '万象双环', '计算光学'],
    isNew: false,
    discount: false
  },
  {
    id: 'nova11',
    name: 'HUAWEI Nova 11',
    price: '2499',
    originalPrice: '2799',
    description: '轻薄设计，前置人像旗舰',
    image: 'https://via.placeholder.com/300x300/FF9500/FFFFFF?text=Nova+11',
    features: ['前置人像', '轻薄设计', '超快充'],
    isNew: false,
    discount: true
  },
  {
    id: 'mate40-pro',
    name: 'HUAWEI Mate 40 Pro',
    price: '5999',
    description: '经典旗舰，徕卡影像',
    image: 'https://via.placeholder.com/300x300/5856D6/FFFFFF?text=Mate40+Pro',
    features: ['徕卡影像', '麒麟芯片', '星环设计'],
    isNew: false,
    discount: false
  }
])

const viewDetail = (phoneId) => {
  router.push(`/goods/${phoneId}`)
}

const addToCart = async (phone) => {
  if (!authStore.isLoggedIn) {
    // 用户未登录，提示登录
    try {
      await ElMessageBox.confirm(
          '请先登录后再添加商品到购物车',
          '登录提示',
          {
            confirmButtonText: '立即登录',
            cancelButtonText: '取消',
            type: 'warning'
          }
      )
      // 用户点击立即登录，显示登录弹窗
      const event = new CustomEvent('showLoginDialog')
      window.dispatchEvent(event)
    } catch {
      // 用户点击取消
      ElMessage.info('已取消添加购物车')
    }
    return
  }

  try {
    cartStore.addToCart({
      id: phone.id,
      name: phone.name,
      price: phone.price,
      image: phone.image,
      description: phone.description
    }, 1)
    ElMessage.success(`已添加 ${phone.name} 到购物车`)
  } catch (error) {
    ElMessage.error(error.message)
  }
}
</script>

<style scoped>
.phones-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 20px 0;
}

.page-header h1 {
  color: #c7000a;
  font-size: 32px;
  margin-bottom: 10px;
}

.page-header p {
  color: #666;
  font-size: 16px;
}

.phone-card {
  margin-bottom: 20px;
  transition: all 0.3s;
  cursor: pointer;
}

.phone-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.product-image-container {
  position: relative;
}

.product-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.product-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #ff4757;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.product-badge.discount {
  background: #ffa502;
  top: 40px;
}

.product-info {
  padding: 15px;
}

.product-info h3 {
  margin: 0 0 10px;
  color: #333;
  font-size: 18px;
  height: 24px;
  overflow: hidden;
}

.product-desc {
  color: #666;
  margin-bottom: 10px;
  font-size: 14px;
  height: 40px;
  overflow: hidden;
  line-height: 1.4;
}

.product-price {
  margin: 10px 0;
}

.current-price {
  color: #c7000a;
  font-size: 20px;
  font-weight: bold;
}

.original-price {
  color: #999;
  text-decoration: line-through;
  margin-left: 8px;
  font-size: 14px;
}

.product-features {
  margin: 10px 0;
}

.feature-tag {
  display: inline-block;
  background: #e3f2fd;
  color: #1976d2;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 12px;
  margin-right: 5px;
  margin-bottom: 5px;
}

.product-actions {
  display: flex;
  gap: 8px;
  margin-top: 15px;
}

.product-actions .el-button {
  flex: 1;
}
</style>